//
//  BlacklistVC.m
//  jinwang
//
//  Created by jzb on 2018/3/13.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "BlacklistVC.h"
#import "BlacklistCell.h"
#import "BlacklistModel.h"

@interface BlacklistVC ()

@property (nonatomic,strong) BlacklistCell *cell;
@property (nonatomic,strong) NSMutableArray *ListmodelArr;

@end

@implementation BlacklistVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"黑名单";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpTableView];
    if ([_vcType isEqualToString:@"黑名单"]) {
        [self makeUrl_user_blackList];
    }else if ([_vcType isEqualToString:@"只给一窗"]){
        [self makeUrl_user_oneList];
    }
}
//配置tableView
-(void)setUpTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    
    [self registerCellWithCellClassArray:@[NSStringFromClass([BlacklistCell class])]];
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if ([_vcType isEqualToString:@"黑名单"]) {
            [self makeUrl_user_blackList];
        }else if ([_vcType isEqualToString:@"只给一窗"]){
            [self makeUrl_user_oneList];
        }
    }];
    
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.ListmodelArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:[BlacklistCell getCellReuseIdentifier]];
    self.cell.contentView.backgroundColor = [UIColor clearColor];

    BlacklistModel *model = self.ListmodelArr[indexPath.row];
    self.cell.model = model;
    
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 81;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView setEditing:NO animated:YES];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if ([_vcType isEqualToString:@"黑名单"]) {
            [self makeUrl_user_delBlackIndexPath:indexPath];
        }else if ([_vcType isEqualToString:@"只给一窗"]){
            [self makeUrl_user_delOneIndexPath:indexPath];
        }
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"移除";
}

//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}
//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}
#pragma mark -网络请求
//黑名单
-(void)makeUrl_user_blackList{
    [[DDNetworkManagerDate makeUrl_user_blackListWithtoken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        NSLog(@"----->>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                BlacklistModel *model = [BlacklistModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
            self.ListmodelArr = arr.mutableCopy;
        }
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//黑名单删除
- (void)makeUrl_user_delBlackIndexPath:(NSIndexPath *)indexPath{
    
    BlacklistModel *model = [BlacklistModel new];
    model = _ListmodelArr[indexPath.row];
    [[DDNetworkManagerDate makeUrl_user_delBlackWithtoken:k_user_token user_id:[model.user_id stringValue]] post_RequestFinshSuccess:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            [_ListmodelArr removeObjectAtIndex:indexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//只给一窗
-(void)makeUrl_user_oneList{
    [[DDNetworkManagerDate makeUrl_user_oneListWithtoken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        NSLog(@"----->>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                BlacklistModel *model = [BlacklistModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
            self.ListmodelArr = arr.mutableCopy;
        }
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//只给一窗删除
- (void)makeUrl_user_delOneIndexPath:(NSIndexPath *)indexPath{
    
    BlacklistModel *model = [BlacklistModel new];
    model = _ListmodelArr[indexPath.row];
    [[DDNetworkManagerDate makeUrl_user_delOneWithtoken:k_user_token user_id:[model.user_id stringValue]] post_RequestFinshSuccess:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            [_ListmodelArr removeObjectAtIndex:indexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
